package stack;

/**
 * 使用数组模拟栈
 * top表示栈顶
 * 入栈 top++;stack[top]=data;
 */
public class Stack {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(3);
        arrayStack.push(5);
        arrayStack.push(4);
        arrayStack.push(3);
        arrayStack.list();
    }
}
class ArrayStack{
    private int maxSize;//栈的大小
    private int[]   stack;//数组模拟栈，数据就放在改数组
    private int top=-1; //top 表示栈顶 初始化为-1
    //构造器
    public ArrayStack(int maxSize ){
        this.maxSize=maxSize;
        stack=new int[this.maxSize];
    }
    //栈满
    public boolean isFull(){
        return top==maxSize-1;
    }
    //栈空
    public boolean isEmpty(){
        return top==-1;
    }
    //入栈 push
    public void push(int value){
        //先判断是不是满了
        if (isFull()){
            return;
        }
        top++;
        stack[top]=value;
    }
    public int pop(){
        if (isEmpty()){
            throw new RuntimeException();
        }
        int value = stack[top];
        top--;
        return value;
    }
    public void list(){
        if (isEmpty()){
            throw new RuntimeException();
        }

        for (int i=top ;i>=0;i--){
            System.out.println(stack[i]);
        }
    }


    public int peek() {
        return stack[top];
    }
}
